package simulator.ui;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

import simulator.Scenario;
import simulator.ScenarioFileLoader;

public class OpenAction extends AbstractAction
{
   private static final long serialVersionUID = 1L;
   private JFrame parent = null;
   private SimulationController controller = null;

   public OpenAction(SimulationController controller, JFrame parent)
   {
      this.parent = parent;
      this.controller = controller;
      putValue(Action.NAME, "Open Scenario...");
      putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
      putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, KeyEvent.VK_O);

      ImageLoader loader = new ImageLoader();
      Icon icon = new ImageIcon(loader.load("open.png"));
      //Icon icon = new ImageIcon("images/open.png");
      putValue(Action.LARGE_ICON_KEY, icon);

      putValue(Action.SMALL_ICON, icon);
   }

   public void actionPerformed(ActionEvent e)
   {
      FileDialog fd = new FileDialog(parent);

      fd.setVisible(true);

      if (fd.getFile() != null)
      {
         Scenario scenario = ScenarioFileLoader.loadScenario(new File(fd.getDirectory() + fd.getFile()));
         controller.setScenario(scenario);
      }
   }
}
